﻿using System;
using Myotragus.Data;
using Myotragus.Data.Containers;
using Myotragus.Data.Domain;
using Myotragus.Store.Model.Tests.Domain;
using NHibernate.Criterion;
using NUnit.Framework;

namespace Myotragus.Store.Model.Tests
{
  [TestFixture]
  [Category("ExternalData")]
  public class LocalizableDataAccessFixtures
  {
    private static readonly Type TypeOfILocalizableObject= typeof(ILocalizableObject);
    public IEntitiesContainer Entities
    { get; set; }

    [SetUp]
    public void SetUp()
    {
      FixtureHelper.InitializeServices();
      FixtureHelper.InitializeEntities(new[] {TypeOfILocalizableObject});
      FixtureHelper.InitializeSession(new[] { TypeOfILocalizableObject });
      
      var registrarType = FixtureHelper.Container.Resolve<IEntitiesRegistrar>();
      Entities = registrarType;
    }

    [Test]
    public void CriteriaTest()
    {
      var save = Entities.Create<ISaveEntity<ILocalizableObject>>();
      var factory = FixtureHelper.Container.Resolve<ISessionFactoryAdapter>();

      using (var session = factory.OpenSession())
      {
        var criteria = session.CreateCriteria(save.GetType()).
          SetProjection(Projections.RowCount());

        var result = criteria.UniqueResult<int>();
      }
    }
  }
}
